defmodule Redis do

  def start(options \\ []) do
    GenServer.start Redis.Server, options, [name: :redis]
  end

  def start_link(options \\ []) do
    GenServer.start_link Redis.Server, options, [name: :redis]
  end

  def set(pid \\ nil, key, value) do
    call(pid, ["set", key, value]) |> sts_reply
  end

  def get(pid \\ nil, key) do
    call(pid, ["get", key])
  end

  def stop(pid) do
    GenServer.cast(pid || client_pid, {:stop})
  end

  defp call(pid, request) do
    GenServer.call(pid || client_pid, {:request, request}) |> elem(1)
  end

  defp sts_reply("OK"), do: :ok

  @spec client_pid() :: pid
  defp client_pid do
    Process.whereis(:redis)
  end
end
